<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-13
 * Time: 9:55:00
 * Info:
 */

namespace plugin\huiblog\app\controller\module;

use support\Request;
use plugin\huiblog\app\model\Keywords as KeywordsModel;
use plugin\huiblog\app\common\CacheClear;

class KeywordsController
{

    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $limit = (int)$request->input('limit', 15);
            $page  = (int)$request->input('page', 1);
            $key   = $request->input('key');
            $first = ($page - 1) * $limit;

            $where = function ($query) use ($key) {
                if ( ! empty($key['title'])) {
                    $query->whereLike('title', "%".$key['title']."%");
                }
            };
            $count = KeywordsModel::where($where)->count();
            $data  = KeywordsModel::where($where)->limit($first,
                $limit)->order('sort asc,id desc')->select()->toArray();

            return json(['code' => 0, 'count' => $count, 'data' => $data, 'msg' => 'ok']);
        }

        return view('admin/keywords/index');
    }

    public function add(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '关键词不能为空']);
            }
            KeywordsModel::create($param);
            CacheClear::clearKeywordsCount();

            return json(['code' => 200, 'msg' => '操作成功']);
        }

        return view('admin/keywords/add');
    }

    public function edit(Request $request)
    {
        $id = $request->get('id');
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['id'])) {
                return json(['code' => 0, 'msg' => '参数错误']);
            }
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '关键词不能为空']);
            }
            KeywordsModel::update($param, ['id' => $param['id']]);

            return json(['code' => 200, 'msg' => '操作成功']);
        }
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        $data = KeywordsModel::find($id);
        if (empty($data)) {
            return json(['code' => 0, 'msg' => '获取数据失败']);
        }

        return view('admin/keywords/edit', ['data' => $data]);
    }

    public function delete(Request $request)
    {
        $keywordsModel = new KeywordsModel();
        $id            = $request->get('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        KeywordsModel::where('id', $id)->delete();
        //删除文章关联关键词
        $keywordsModel->deleteKeyword($id);
        CacheClear::clearKeywordsCount();

        return json(['code' => 200, 'msg' => '删除成功']);
    }

    public function get_keyword_cate()
    {
        $data = KeywordsModel::where('status', 1)->where('delete_time', 0)->select();

        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

}
